package com.kolon.business.mngr.banner.controller;

import java.util.HashMap;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.kolon.business.mngr.banner.dao.BannerDao;
import com.kolon.framework.file.FileUtil;
import com.kolon.framework.interfaces.SimpleController;
import com.kolon.framework.util.ComUtil;
import com.kolon.framework.util.MsgUtil;

@Controller("com.kolon.business.mngr.banner.controller.BannerDeleteController")
public class BannerDeleteController implements SimpleController {

	@Resource(name="com.kolon.business.mngr.banner.dao.BannerDaoImpl")
	private BannerDao bannerDao;
	
	@SuppressWarnings("rawtypes")
	@RequestMapping(value="/business/mngr/banner/bannerDelete.do")
	public ModelAndView controller(HttpServletRequest request,
		HttpServletResponse response) throws Exception {
		
		Map<String, String> map = new HashMap<String, String>();
		// 파라미터
		int page = ComUtil.sToi(ComUtil.nullChk(request.getParameter("page"), "1"));
		String bnerNo = ComUtil.nullChk(request.getParameter("bnerNo"));
		String schTp = ComUtil.nullChk(request.getParameter("schTp"));
		String schTxt = ComUtil.nullChk(request.getParameter("schTxt"));
		// 파일 저장 경로
		String bannerPath = MsgUtil.getMessage("banner.path");
		map.put("bnerNo", bnerNo);
		map.put("schTp", schTp);
		map.put("schTxt", schTxt);
		// 정보 조회
		Map bnerMap = bannerDao.getMap(map);
		if (bnerMap != null) {
			// 데이터 및 이미지 삭제
			bannerDao.delete(map);
			FileUtil.delFile(bannerPath + bnerMap.get("IMG_NM"));
		}
		// 리턴
		ModelAndView modelAndView = new ModelAndView();
		modelAndView.setViewName("redirect:/business/mngr/banner/bannerList.do");
		modelAndView.addObject("page", page);
		modelAndView.addObject("schTp", schTp);
		modelAndView.addObject("schTxt", schTxt);
		return modelAndView;
	}
}
